gusucode.com > VC 点对点文件传输实例 > VC 点对点文件传输实例/code/www.NewXing.com/Server_FileTransfer/Server_FileTransferDlg.cpp
// Server_FileTransferDlg.cpp : implementation file // Download by http://www.NewXing.com #include "stdafx.h" #include "Server_FileTransfer.h" #include "Server_FileTransferDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CServer_FileTransferDlg dialog CServer_FileTransferDlg::CServer_FileTransferDlg(CWnd* pParent /*=NULL*/) : CDialog(CServer_FileTransferDlg::IDD, pParent) { //{{AFX_DATA_INIT(CServer_FileTransferDlg) // NOTE: the ClassWizard will add member initialization here m_pServer = NULL;//指针置空——安全 //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CServer_FileTransferDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CServer_FileTransferDlg) DDX_Control(pDX, IDC_STATIC_LISTENSTATUS, m_staticListenStatus); DDX_Control(pDX, IDC_LIST_FILELIST, m_ListFile); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CServer_FileTransferDlg, CDialog) //{{AFX_MSG_MAP(CServer_FileTransferDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_ADDFILE, OnButtonAddfile) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit) ON_NOTIFY(NM_RCLICK, IDC_LIST_FILELIST, OnRclickListFilelist) //}}AFX_MSG_MAP ON_MESSAGE(WM_STARTLISTEN, OnStartListen) ON_MESSAGE(WM_SENDFILELIST, OnSendFileList) //消息映射 END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CServer_FileTransferDlg message handlers BOOL CServer_FileTransferDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here InitListColumn(); //初始化文件列表 m_pServer = new CTCPServer_FT(this);//初始化对象指针,为this if(m_pServer != NULL) { m_pServer->SetListenPort(7000);//设置端口 m_pServer->ListenRequest();//开始监听 } return TRUE; // return TRUE unless you set the focus to a control } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CServer_FileTransferDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CServer_FileTransferDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //初始化列表函数 void CServer_FileTransferDlg::InitListColumn() { int iWidth = 50; m_ListFile.InsertColumn(0, "File Name", LVCFMT_LEFT, 3*iWidth, -1); m_ListFile.InsertColumn(1, "File Size", LVCFMT_LEFT, 2*iWidth, -1); m_ListFile.InsertColumn(2, "Full Path", LVCFMT_LEFT, 5*iWidth, -1); //插入列:左对齐,无子列 m_ListFile.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); //设置列表扩展样式:整行选中|带网格 } //添加文件 void CServer_FileTransferDlg::OnButtonAddfile() { // TODO: Add your control notification handler code here CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT, "All Files(*.*)|*.*||", this); //创建一个打开对话框,只读|可多选,所有类型,获得当前指针 if(dlg.DoModal() == IDOK) { POSITION pos = dlg.GetStartPosition();//返回文件列表中第一个文件的位置 while(pos != NULL)//文件位置存在 { CString strPathName = dlg.GetNextPathName(pos);//获取下一个文件路径 CFile file; BOOL bResult = file.Open(strPathName, CFile::modeRead|CFile::shareDenyNone, NULL); //打开指定文件 if(!bResult)//打开不成功 continue; //结束本次循环,继续下次循环 CString strFileName = file.GetFileName();//得到文件名 long lFileLength = file.GetLength(); file.Close(); CString strFileSize;//显示文件大小 strFileSize.Format("%0.2fk", (float)lFileLength/1024); //精确到小数点后两位,单位字节 int iItem = m_ListFile.GetItemCount();//获取文件列表元素个数 LV_ITEM lvi;//结构体ListView Item,指定或接收属性 lvi.mask = LVIF_TEXT|LVIF_PARAM; lvi.iItem = iItem; lvi.iSubItem = 0; lvi.lParam = lFileLength; lvi.pszText = strFileName.GetBuffer(0);//返回CString中指向字符串的指针 m_ListFile.InsertItem(&lvi);//插入列表 m_ListFile.SetItemText(iItem, 1, strFileSize.GetBuffer(0)); m_ListFile.SetItemText(iItem, 2, strPathName.GetBuffer(0)); } } } //消息函数 LRESULT CServer_FileTransferDlg::OnStartListen(WPARAM wParam, LPARAM lParam) { m_staticListenStatus.SetWindowText("正在监听!"); return 0; } LRESULT CServer_FileTransferDlg::OnSendFileList(WPARAM wParam, LPARAM lParam) { SOCKET sock = *(SOCKET*)wParam;//创建socket int iTotal = m_ListFile.GetItemCount(); MSGFILELIST msgFileList;//结构体,文件列表缓冲区 for(int i=0; i<iTotal; i++) { msgFileList.lFileLength = m_ListFile.GetItemData(i);//文件长度 strcpy(msgFileList.sServerPath, m_ListFile.GetItemText(i, 2)); //文件路径 int iSendCnt = send(sock, (char*)&msgFileList, sizeof(msgFileList), 0); //发送至socket } return 0; } //清除工作 //OnDestroy is called after the CWnd object is removed from the screen. void CServer_FileTransferDlg::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here if(m_pServer != NULL) { m_pServer->StopListen(); delete m_pServer; } } void CServer_FileTransferDlg::OnButtonExit() { // TODO: Add your control notification handler code here OnOK(); } void CServer_FileTransferDlg::OnRclickListFilelist(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here *pResult = 0; }